﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CompanyBussiness;

namespace Website.customer.Control
{
    public partial class cQuickMove : System.Web.UI.UserControl
    {
        ProductManagement proMana = new ProductManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        TurnOverService overServices = new TurnOverService();
        CompanyBussiness.Product pro = new Product();
        CompanyBussiness.Language language;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies[CONST.COOKIES_LANGUAGE] == null)
            {
                Session[CONST.SESSION_LANGUAGE] = "";
                language = languageMana.GetLanguageByCode("vi-VN");
            }
            else
            {
                Session[CONST.SESSION_LANGUAGE] = Request.Cookies[CONST.COOKIES_LANGUAGE][CONST.COOKIES_LANGUAGE_ID];
                language = languageMana.GetLanguageByID(int.Parse(Session[CONST.SESSION_LANGUAGE].ToString()));
            }
            
            if (!Page.IsPostBack)
            { 
                LoadDropParent();
            }
        }

        

        protected void LoadDropParent()
        {
            dropParentTypeProduct.Items.Clear();
            dropParentTypeProduct.DataSource = proMana.GetParentTypeProductAll(language.LanguagesID);
            dropParentTypeProduct.DataValueField = "TypeProductID";
            dropParentTypeProduct.DataTextField = "TypeProductName";
            dropParentTypeProduct.DataBind();
            dropParentTypeProduct.Items.Insert(0,new ListItem("[-----------Sản phẩm---------]","0"));
        }

        protected void LoadDropChildByParentID(int ids)
        {
            dropChildTypeProduct.Items.Clear();
            dropChildTypeProduct.DataSource = proMana.GetChildrenTypeProductByParentID(ids, language.LanguagesID, true);
            dropChildTypeProduct.DataTextField = "TypeProductName";
            dropChildTypeProduct.DataValueField = "TypeProductID";
            dropChildTypeProduct.DataBind();
            dropChildTypeProduct.Items.Insert(0, new ListItem("[-----------Sản phẩm---------]", "0"));
        }


        protected void LoadDropProductByChildID(int ids)
        {
            dropProduct.Items.Clear();
            dropProduct.DataSource = proMana.GetProductByTypeProductIDAll(ids);
            dropProduct.DataTextField = "ProductName";
            dropProduct.DataValueField = "ProductID";
            dropProduct.DataBind();
            dropProduct.Items.Insert(0, new ListItem("[-----------Sản phẩm---------]", "0"));
        }

        protected void dropParentTypeProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.LoadDropChildByParentID(int.Parse(dropParentTypeProduct.SelectedValue.ToString()));
            
        }

        protected void dropChildTypeProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["child"]=int.Parse(dropChildTypeProduct.SelectedValue.ToString());
            this.LoadDropProductByChildID(int.Parse(dropChildTypeProduct.SelectedValue.ToString()));
            dropChildTypeProduct.SelectedValue =(Session["child"].ToString());
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (dropParentTypeProduct.SelectedValue == "0")
            {
                return;
            }
            else if (dropParentTypeProduct.SelectedValue != "0" && dropChildTypeProduct.SelectedValue == "0")
            {
                Response.Redirect("~/Hem/san-pham-dich-vu/" + dropParentTypeProduct.SelectedValue.ToString() + "/" + "MPRODUCT" + "/" + overServices.ConvertString(proMana.GetNametypeByID(int.Parse(dropParentTypeProduct.SelectedValue.ToString())).Replace(" ", "-")) + ".html");
            }
            else if (dropParentTypeProduct.SelectedValue != "0" && dropChildTypeProduct.SelectedValue != "0" && dropProduct.SelectedValue == "0")
            {
                Response.Redirect("~/Hem/san-pham-dich-vu/" + dropChildTypeProduct.SelectedValue.ToString() + "/" + "MPRODUCT" + "/" + overServices.ConvertString(proMana.GetNametypeByID(int.Parse(dropChildTypeProduct.SelectedValue.ToString())).Replace(" ", "-")) + ".html");
            }
            else if (dropParentTypeProduct.SelectedValue != "0" && dropChildTypeProduct.SelectedValue != "0" && dropProduct.SelectedValue != "0")
            {
                Response.Redirect("~/Hem/" + dropProduct.SelectedValue.ToString() + "_" + dropChildTypeProduct.SelectedValue.ToString() + "/San-pham-dich-vu/" + "MPRODUCT" + "/" + overServices.ConvertString(proMana.GetNameProductByID(int.Parse(dropProduct.SelectedValue.ToString())).Replace(" ", "-")) + ".html");
            }
        }

    }
}